/*
 * @Version: 1.0
 * @Autor: yuchang
 * @Date: 2023-03-02 11:09:09
 * @LastEditors: yuchang
 * @LastEditTime: 2024-01-16 16:58:48
 * @Description:
 */
import sonIconBig from '@/assets/Icon/sonIconBig.png'
import { Button, Form, Input, message, Select } from 'antd'
import { connect } from 'dva'
import React from 'react'
import styles from './styles.less'
function mapStateToProps({ globalModel, campusOrgModel }) {
  return {
    globalModel: globalModel,
    campusOrgModel: campusOrgModel,
  }
}
@connect(mapStateToProps)
class Details extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      form: null,
      provin: '',
      city: '',
    }
  }
  //渲染之前
  componentWillMount() {}
  //渲染之后
  componentDidMount() {}
  //卸载
  componentWillUnmount() {}
  componentDidUpdate(prevProps, prevState, snapshot) {}
  //地区点击
  cityChange = (value, selectedOptions) => {
    this.setState({
      provin: selectedOptions[0]?.label,
      city: selectedOptions[1]?.label,
    })
  }
  //编辑点击
  updateFun = () => {
    this.props.dispatch({
      type: 'campusOrgModel/setUpdateVisible',
      payload: false,
    })
  }
  //取消
  onCancel = () => {
    this.props.dispatch({
      type: 'campusOrgModel/setUpdateVisible',
      payload: true,
    })
  }
  //保存
  saveFun = () => {
    let { form, provin, city } = this.state
    let { checkOrg } = this.props.campusOrgModel
    form
      ?.validateFields()
      .then((value) => {
        console.log(value, form, 'valuevalue')
        if (value.type === 1) {
          if (value.name === '') {
            message.error('请输入分校名称')
          } else {
            //修改组织
            this.props.dispatch({
              type: 'campusOrgModel/putOrganizationEffects',
              payload: {
                params: {
                  id: checkOrg.key,
                  parentId: checkOrg.parentId,
                  name: value.name,
                  type: value.type,
                  status: 1,
                },
                callback: (data) => {
                  let { getTree } = this.props
                  if (getTree) getTree()
                  this.props.dispatch({
                    type: 'campusOrgModel/setCheckOrg',
                    payload: { ...checkOrg, name: value.name, type: value.type },
                  })
                  message.success('保存成功')
                  this.onCancel()
                },
              },
            })
          }
        } else if (value.type === 2) {
          if (value.name === '') {
            message.error('请输入院校名称')
          } else {
            //修改组织
            this.props.dispatch({
              type: 'campusOrgModel/putOrganizationEffects',
              payload: {
                params: {
                  id: checkOrg.key,
                  parentId: checkOrg.parentId,
                  name: value.name,
                  type: value.type,
                  status: 1,
                },
                callback: (data) => {
                  let { getTree } = this.props
                  if (getTree) getTree()
                  this.props.dispatch({
                    type: 'campusOrgModel/setCheckOrg',
                    payload: { ...checkOrg, name: value.name, type: value.type },
                  })
                  message.success('保存成功')
                  this.onCancel()
                },
              },
            })
          }
        }
      })
      .catch((res) => {
        console.log(res, 111)
      })
  }

  render() {
    let { form } = this.state
    let { checkOrg, updateVisible } = this.props.campusOrgModel
    return (
      <React.Fragment>
        <div className={styles.normal} key={checkOrg.id}>
          <div className={styles.round}>
            <img alt="dep" src={sonIconBig} style={{ width: '24px', height: '24px' }} />
          </div>
          <Form
            key={updateVisible}
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 16 }}
            layout="vertical"
            ref={(event) => {
              if (form !== event) {
                this.setState({ form: event })
              }
            }}
            onFinish={this.onFinish}
            onFinishFailed={(info) => {
              console.log(info, 'infoinfo')
            }}>
            <Form.Item wrapperCol={{ span: 16 }} label="组织类型" name="type" initialValue={checkOrg?.type}>
              <Select
                className={styles.mySelect}
                disabled={true}
                defaultValue={checkOrg?.type}
                onChange={(value) => {
                  this.setState({ type: value })
                }}
                options={[
                  { label: '学校', value: 1 },
                  { label: '院校', value: 2 },
                ]}
              />
            </Form.Item>
            {checkOrg?.type === 1 ? (
              <div>
                <Form.Item
                  wrapperCol={{ span: 16 }}
                  label={checkOrg?.parentId == 0 ? '学校名称' : '分校名称'}
                  name="name"
                  initialValue={checkOrg?.name}>
                  <Input
                    style={{ boxShadow: 'none', borderColor: '#d9d9d9', paddingLeft: '0' }}
                    disabled={updateVisible}
                    className={styles.noBorder}
                    defaultValue={checkOrg?.name}
                  />
                </Form.Item>
                <Form.Item
                  wrapperCol={{ span: 16 }}
                  label="上级组织"
                  name="parentName"
                  initialValue={checkOrg?.parentName ? checkOrg?.parentName : '无'}>
                  <Input
                    style={{ boxShadow: 'none', borderColor: '#d9d9d9', paddingLeft: '0' }}
                    disabled={true}
                    className={styles.noBorder}
                    defaultValue={checkOrg?.parentName ? checkOrg?.parentName : '无'}
                  />
                </Form.Item>
              </div>
            ) : (
              <div>
                <Form.Item wrapperCol={{ span: 16 }} label="院校名称" name="name" initialValue={checkOrg?.name}>
                  <Input
                    style={{ boxShadow: 'none', borderColor: '#d9d9d9', paddingLeft: '0' }}
                    disabled={updateVisible}
                    className={styles.noBorder}
                    defaultValue={checkOrg?.name}
                  />
                </Form.Item>
                <Form.Item
                  wrapperCol={{ span: 16 }}
                  label="上级组织"
                  name="parentName"
                  initialValue={checkOrg?.parentName ? checkOrg?.parentName : '无'}>
                  <Input
                    style={{ boxShadow: 'none', borderColor: '#d9d9d9', paddingLeft: '0' }}
                    disabled={true}
                    className={styles.noBorder}
                    defaultValue={checkOrg?.parentName ? checkOrg?.parentName : '无'}
                  />
                </Form.Item>
              </div>
            )}
          </Form>
          {updateVisible && (
            <Button onClick={this.updateFun} type="primary" style={{ borderRadius: '4px' }}>
              编辑
            </Button>
          )}
          {!updateVisible && (
            <div>
              <Button onClick={this.saveFun} type="primary" style={{ borderRadius: '4px', marginRight: '20px' }}>
                保存
              </Button>
              <Button onClick={this.onCancel} style={{ borderRadius: '4px' }}>
                取消
              </Button>
            </div>
          )}
        </div>
      </React.Fragment>
    )
  }
}
export default Details
